import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { CacheManager } from '@midwayjs/cache';
import { Context } from '@midwayjs/koa';
import { BusinessProductAListEntity } from '../entity/product/a_list';
import { BusinessProductBListEntity } from '../entity/product/b_list';
import { BusinessGalleryEntity } from '../entity/gallery';

/**
 * 统计
 */
@Provide()
export class BusinessStatisticsService extends BaseService {

  @InjectEntityModel(BusinessGalleryEntity)
  businessGalleryEntity: Repository<BusinessGalleryEntity>;

  @InjectEntityModel(BusinessProductAListEntity)
  businessProductAListEntity: Repository<BusinessProductAListEntity>;

  @InjectEntityModel(BusinessProductBListEntity)
  businessProductBListEntity: Repository<BusinessProductBListEntity>;

  @Inject()
  cacheManager: CacheManager;

  @Inject()
  ctx: Context;

  // 基础统计信息
  async getBase() {
    const { userId } = this.ctx.admin;
    const ProductATotal = await this.businessProductAListEntity.count({
      where: { userId: userId }
    })
    const ProductBTotal = await this.businessProductBListEntity.count({
      where: { userId: userId }
    })
    const GalleryTotal = await this.businessGalleryEntity.count({
      where: { userId: userId }
    })
    const data = {
      a: {
        total: ProductATotal
      },
      b: {
        total: ProductBTotal
      },
      gallery: {
        total: GalleryTotal
      }
    }
    return data
  }

}
